package com.huilian.iotbox.admin.service.impl;

import com.huilian.iotbox.admin.service.RoleService;
import com.huilian.iotbox.admin.service.UserRoleService;
import com.huilian.iotbox.data.dao.UserRoleDao;
import com.huilian.iotbox.data.po.Role;
import com.huilian.iotbox.data.po.UserRole;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 少凡
 * @date 2020/3/1 21:51
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Resource
    private UserRoleDao userRoleDao;

    @Resource
    private RoleService roleService;
    @Override
    public Integer countByRoleId(Integer roleId) {
        return userRoleDao.countByRoleId(roleId);
    }


    @Override
    public void add(UserRole userRole) {
        userRoleDao.insert(userRole);
    }

    @Override
    public Integer findByUserIdAndRoleId(UserRole userRole) {
        return userRoleDao.findByUserIdAndRoleId(userRole);
    }

    @Override
    public void delete(UserRole userRole) {
        userRoleDao.delete(userRole);
    }

    @Override
    public void insert(UserRole userRole) {
        userRoleDao.insert(userRole);
    }

    @Override
    public List<UserRole> findUserByRoleId(Integer roleId) {
        return userRoleDao.findUserByRoleId(roleId);
    }

    @Override
    public UserRole findOneUserRoleByUserId(Integer UserId) {
        return userRoleDao.findOneUserRoleByUserId(UserId);
    }

    @Override
    public UserRole findByUserId(Integer userId) {
        return userRoleDao.findByUserId(userId);
    }

    @Override
    public void update(UserRole userRole) {
        userRoleDao.update(userRole);
    }

    @Override
    public List<Integer> findListByUserId(Integer userId) {
        return userRoleDao.findListByUserId(userId);
    }

    //    @Override
//    public Integer getUserRoleType(Integer userId) {
//        List<Role> userRoleList = roleService.getUserRoleList(Integer.valueOf(userId));
//        for (int i = 0; i < 3; i++) {
//            if (roleType(RoleEnum.SUPER_ADMINISTRATOR.getRoleId(), userRoleList) > -1) {
//                return RoleEnum.SUPER_ADMINISTRATOR.getRoleId();
//            }
//            else if (roleType(RoleEnum.AGENT.getRoleId(), userRoleList) > -1) {
//                return RoleEnum.AGENT.getRoleId();
//            }
//            else if (roleType(RoleEnum.MERCHANT.getRoleId(), userRoleList) > -1) {
//                return RoleEnum.MERCHANT.getRoleId();
//            }
//        }
//        return -1;
//    }
    public Integer roleType(Integer code, List<Role> roles) {
        for (Role role : roles) {
            if (code.equals(role.getId())) {
                return code;
            }
        }
        return -1;
    }
}
